package main

import (
	"errors"
	"fmt"
	"gitee.com/go_programming_tour/tour/cmd"
	"log"
)

type Name string

var name string

func main() {

	// 示例4 单词转换
	err := cmd.Execute()
	if err != nil {
		log.Fatalf("cmd.Execute err: %v", err)
	}

	// 示例3 自定义参数类型
	//var myName Name
	//flag.Var(&myName, "name", "help info")
	//flag.Parse()
	//log.Printf("name: %s", myName)

	// 示例2 子命令
	//flag.Parse()
	//args := flag.Args()
	//switch args[0] {
	//case "go":
	//	goCmd := flag.NewFlagSet("go", flag.ExitOnError)
	//	goCmd.StringVar(&name, "name", "go", "help info")
	//	_ = goCmd.Parse(args[1:])
	//case "php":
	//	phpCmd := flag.NewFlagSet("php", flag.ExitOnError)
	//	phpCmd.StringVar(&name, "name", "php", "help info")
	//	_ = phpCmd.Parse(args[1:])
	//}
	//log.Printf("name: %s", name)

	// 示例1
	//flag.StringVar(&name, "name", "go语言编程", "help_info")
	//flag.StringVar(&name, "n", "go语言编程", "help info")
	//flag.Parse() //  解析命令行参数，在参数注册之后调用
	//log.Printf("name: %s", name)
}

func (i *Name) String() string {
	return fmt.Sprint(*i)
}

func (i *Name) Set(value string) error {
	if len(*i) > 0 {
		return errors.New("name flag already set")
	}
	*i = Name("sdf:" + value)
	return nil
}
